clear
set matsize 5000
cd <directory>
set matsize 5000

cap log close

log using "att_tempgradient_onespline_led0_umfe", replace

use "linelevelattendancewithtamudatanew" , clear

**********************************************************************************************************************
**Table 3- Columns 1-2 (Attendance-Temperature Gradient)
**********************************************************************************************************************

*drop post-LED observations
keep if led==0

*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

global depvars mactualeff

*residualize line level daily attendance temperature and rainfall - partial out  production line FE, factory by year, factory by month, day of the week FE

foreach i of varlist wbgtmeant1 wbgtmeant2 meanp precipitation relativehumidity{
areg `i'   _I*,  absorb(rno) cluster(unit)
predict `i'res, res
}

*Bootstraps - Temperature gradient

foreach var of varlist wbgtmeant {
cgmwildboot meanpres `var'1res `var'2res, reps(1000) seed(10101) cluster(unit) bootcluster(unit) null(0 0)

}

*Control for  Rainfall*
foreach var of varlist wbgtmeant {

cgmwildboot meanpres `var'1res `var'2res precipitationres, reps(1000) seed(10101) cluster(unit) bootcluster(unit) null( 0 0 0)

}

cap drop *res


**********************************************************************************************************************
**Table 2 (Columns 3 and 4) - Interactions with the LED adoption dummy
**********************************************************************************************************************

use "linelevelattendancewithtamudatanew.dta" , clear

*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

*Generate LED interaction with spline Variables for Temperature

foreach var of varlist wbgtmeant{
foreach i of numlist 1/2{
g `var'`i'led=`var'`i'*led
}
}

*residualize line level daily attendance temperature and rainfall, including LED interactions and the LED dummy variable - partial out  production line FE, factory by year, factory by month, day of the week FE

foreach var of varlist meant himeant wbgtmeant{
foreach i of numlist 1/2{
areg `var'`i'  _I*,  absorb(rno) cluster(unit)
predict `var'`i'res, resid

areg `var'`i'led  _I*,  absorb(rno) cluster(unit)
predict `var'`i'ledres, resid
}
}

foreach var of varlist meanp precipitation relativehumidity led{
areg `i'   _I*,  absorb(rno) cluster(unit)
predict `i'res, res
}


*Bootstraps - Temperature gradient and LED mitigation

foreach var of varlist wbgtmeant {

cgmwildboot meanpres `var'1ledres `var'2ledres `var'1res `var'2res ledres, reps(1000) seed(10101) cluster(unit) bootcluster(unit) null(0 0 0 0 0 )

}

*Control for  Rainfall*
foreach var of varlist meant himeant wbgtmeant {

cgmwildboot meanpres `var'1ledres `var'2ledres `var'1res `var'2res ledres precipitationres, reps(1000) seed(10101) cluster(unit) bootcluster(unit) null( 0 0 0 0 0 0)

}

cap drop *res


log close
